package com.yin.ketech.config.security;

import com.yin.ketech.entity.user.SysUser;
import com.yin.ketech.mapper.user.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName CustomUserService
 * @Author luzw
 * @Description
 * @Version V1.0
 * @Date 2020/6/2 16:48
 **/
public class CustomUserService implements UserDetailsService {

    @Autowired
    private SysUserMapper userMapper;

    //private SysRoleMapper roleMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser user = userMapper.selectByUsername(username);
        if (user == null) {
            throw new UsernameNotFoundException("用户名不存在！");
        }
        List<SimpleGrantedAuthority> authorities = new ArrayList();
        //List<SysRole> roles = roleMapper.selectRolesByUserId(user.getId());
        //roles.forEach(r -> authorities.add(new SimpleGrantedAuthority(r.getCode())));
        authorities.add(new SimpleGrantedAuthority("system:user:add"));
        return new User(user.getUsername(), user.getPassword(), authorities);
    }
}
